<?php

class Upload extends CI_Controller {
 
	 function __construct(){
			      parent::__construct();
			      $this->load->database();
				  $this->load->helper('url');
				  $this->load->library('pagination');
			      $this->load->helper(array('form', 'url'));
	 }
	 
	 function index(){ 
	  			  $this->load->view('upload_form', array('error' => ' ' ));
	 }

	 function do_upload(){

				  $config['upload_path'] = './upload/image/';
				  $config['allowed_types'] = 'gif|jpg|png|jpeg';
				  $config['max_size'] = '1000';
				  $config['max_width']  = '20480';
				  $config['max_height']  = '20480'; 
				  
				  $this->load->library('upload', $config);
				  
				  if ( ! $this->upload->do_upload()){
						$error = array('error' => $this->upload->display_errors());

					} else {
						$data = array('upload_data' => $this->upload->data());					
					} 
					//exit(var_dump($data));
			if($data == null){
					//$data = array('upload_data' => $this->upload->data());
				    $cid = $this->input->post('cid');
					$this->load->model('articleList_model');
					$id = $this->uri->segment(4);
					$title = $this->input->post('title');
					$author = $this->input->post('author');
					$date = $this->input->post('date');
					$content = $this->input->post('myContent');
					$this->articleList_model->article_update($title, $author, $content, $date, $id);

					redirect('content/articleList/' . $cid);
				
			}else{
				if ( ! $this->upload->do_upload()){
				   $error = array('error' => $this->upload->display_errors());
				   
				   exit("图片添加失败");
				} else {
					//$data = array('upload_data' => $this->upload->data());					
					$url =  base_url() . "upload/image/" . $data['upload_data']['file_name'];
 				   
				    $cid = $this->input->post('cid');
					$this->load->model('articleList_model');
					$id = $this->uri->segment(4);
					$title = $this->input->post('title');
					$author = $this->input->post('author');
					$date = $this->input->post('date');
					$content = $this->input->post('myContent');
					$this->articleList_model->article_update($title, $author, $content, $date, $id, $url);

					redirect('content/articleList/' . $cid);

				}
			}	  
	 } 
}
?>